﻿using Backend.Data.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;

namespace Backend.Data.Mappings
{
    class ApplicationUserMap : EntityTypeConfiguration<ApplicationUser>
    {
        public ApplicationUserMap()
        {
            //table
            //ToTable("AspNetUsers");

            //pk
            HasKey(t => t.Id);

            //property
            //Property(t => t.Id)
            //    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            Property(t => t.IsPasswordChangeRequired)
                .IsRequired();

            //relationship
            /* 1 to 0.1
            HasRequired(t => t.NaturalPerson)
                .WithRequiredDependent(c => c.ApplicationUser)
                .WillCascadeOnDelete(false);
                */

            // esta relacion es 0.1 to 0.1 caso atípico, normalmente un User es un NaturalPerson
            HasOptional(t => t.NaturalPerson)
                .WithOptionalDependent(c => c.ApplicationUser)
                .WillCascadeOnDelete(false);

            // 0.* to 0.*
            HasMany(x => x.GeoReferencias)
               .WithMany(y => y.ApplicationUsers)
               .Map(m=>{
                   m.ToTable("AspNetUserGeoReferencias");
                   m.MapLeftKey("UserId");
                   m.MapRightKey("GeoReferenciaId");
               });


        }
    }
}
